package com.yhgm.controller;

import com.yhgm.common.BaseResponse;
import com.yhgm.data.ReqCopy;
import com.yhgm.pojo.Image;
import com.yhgm.service.ImageService;
import com.yhgm.utils.AliOssUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.prefs.BackingStoreException;

/**
 * @Author: yh
 * @Date: 2024/4/25
 */
@RestController
@RequestMapping("/file")
public class ImageController {
    @Resource
    private ImageService imageService;

    /*
    万能模糊查询
     */
    @PostMapping("findByCondition")
    public BaseResponse<List<Image>> findByCondition(@RequestBody Image image){
        return imageService.findByCondition(image);
    }

    /**
     * 上传文件
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("upload")
    public String uploadFile(MultipartFile file) throws IOException {
        return imageService.uploadFile(file);
    }

    /**
     * 上传到数据库image
     * @param image
     * @return
     */
    @PostMapping("add")
    public BaseResponse<Integer> addFile(@RequestBody Image image) {
        return imageService.addFile(image);
    }

    /**
     * 展示未删除文件夹
     * @param path 绝对路径
     * @return
     */
    @PostMapping("list")
    public BaseResponse<List<Image>> listFile(@RequestParam("path") String path){
        return imageService.listFile(path);
    }

    /**
     * 伪删除
     * @param ids
     * @return
     */
    @PostMapping("del")
    public BaseResponse<Long> delFile(@RequestParam("ids") Long[] ids){
        return imageService.delFile(ids);
    }

    /**
     * 更新image
     * @param image
     * @return
     */
    @PostMapping("update")
    public BaseResponse<Integer> updateFile(@RequestBody Image image) {
        return imageService.updateFile(image);
    }

    /**
     * 回收站API
     */
    /**
     * 展示伪删除文件
     * @param path
     * @return
     */
    @PostMapping("listRecycle}")
    public BaseResponse<List<Image>> listRecycle(@RequestParam("path") String path){
        return imageService.listRecycle(path);
    }

    /**
     * 恢复伪删除文件
     * @param ids
     * @return
     */
    @PostMapping("recover")
    public BaseResponse<Integer> recoverImages(@RequestParam("ids") Long[] ids){
        return imageService.recoverImages(ids);
    }

    /**
     * 删除伪删除文件
     * @param ids
     * @return
     */
    @PostMapping("delRecycle")
    public BaseResponse<Long> delRecycle(@RequestParam("ids") Long[] ids){
        return imageService.delRecycle(ids);
    }

    /**
     * 复制
     * 有个源地址和目标地址
     * 假如说得到了文件，则目标地址加 /filename 递归进去 把目标地址换一下
     */
    @PostMapping("copy")
    public BaseResponse<Long> copyFile(@RequestBody ReqCopy reqCopy) throws Exception {
        return imageService.copyFile(reqCopy);
    }
}
